u gimport java.sql.SQLOutput;
import java.util.Arrays;

public class Test {
    public static void main(String[] args) {
        int[] array1 = {1 ,2 ,3 ,4 };
        int[] copy = new int[array1.length];
        for (int i = 0 ; i < array1.length ; i++){
            copy[i] = array1[i];
        }
        System.out.println(Arrays.toString(copy));
        System.out.println("===============");
        int [] copy2 = Arrays.copyOf(array1,array1.length*2);//copyOf也可以当成一种扩容
        int [] copy3 = Arrays.copyOfRange(array1,0,3);//把array1中的第一个数到第三个数拷贝到copy3中
        System.out.println(Arrays.toString(copy2));
    }
    public static void main1(String[] args) {
        int[] array = {1,5,2,3,8};
        //int[] array = {};
        //int[] array = null;

        System.out.println(myToString(array));
        bubbleSort(array);
        //Arrays.sort(array,0,3);//java也是左闭右开
        System.out.println(myToString(array));
    }
    //数组的拷贝

    //用java写冒泡排序
    public static void bubbleSort(int[] array){
        for(int i = 0 ; i < array.length-1 ; i++){//走N-1趟
            boolean flag = false;
            for(int j = 0 ; j < array.length-i-1;j++){
                if(array[j]>array[j+1]){
                    int tmp = array[j];
                    array[j] = array[j+1];
                    array[j+1] = tmp;
                    flag = true;
                }
            }
            if(!flag){//没有交换
                break;//return;
            }
        }
    }
    public static String myToString(int[] array){//自己写一个打印数组的方法
        if(array == null){
            return "null";
        }
        if(array.length == 0){
            return "[]";
        }
        String ret="[";
        for(int i = 0;i<array.length;i++){
            ret+=array[i];
            if(i != array.length-1){
                ret+=", ";
            }
        }
        ret+="]";
        return ret;
    }
}

